﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Engine.Data;
using Agile.Engine.Utils;

namespace Fis.Web.Rules.Book
{
    public class LoginRule : DynamicRule
    {
        public override void ExecuteNonTrans()
        {
            /* 规则：
                1，取 Service 信息 中 default 帐套
             *  2, 有效返回 帐套 和 登录信息
             *  2, 用户验证有效返回 nobook
            */

            CommonRule common = new CommonRule();

            var xml = Agile.Engine.ObjectContext.GetResource("ServiceResource");
            var sql = xml.Merge("login_valid", Paramater);

            var data = Model.QueryTable(System.Data.CommandType.Text, "TUSER", sql);

            ///不存在
            if (data.Rows.Count == 0)
            {
                Status = -1;
                Message = "用户名/密码错误";
            }
            else
            {
                var row = data.Rows[0];
                ///账户禁用
                if (Agile.Engine.Utils.DataUtils.Parse(0, row["FSTATUS"]) < 0)
                {
                    Status = -1;
                    Message = "用户被禁用";
                }
                else
                {
                    Status = 1;
                    Result["USERSID"] = data.Rows[0]["SID"];
                    using (var stream = IOUtils.InputStream(common.BookRealPath(Paramater["SBOOKID"].ToString(), BookYear)))
                    {
                        var b = ZipUtils.Zip(stream);
                        Result["cache"] = Convert.ToBase64String(b);
                    }
                }
            }
        }
    }
}
